//
//  SDPaientEditorViewController.m
//  SKinDiary
//
//  Created by pcyang on 16/7/6.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDPaientEditorViewController.h"
#import "ZHDatePicker.h"
#import "SDCustomFieldView.h"
#import "SDPaientInfoItem.h"
#import "SDPaientInfoMgr.h"
#import "SDPhotoInfo.h"
#import "SDEditPaientInfoData.h"
#import "AliyunOSSManager.h"

#define kDefaultAvatarName      @"img_profile_avator_defult.png"

#define kFiedViewMarginLeftWidth 18

@interface SDPaientEditorViewController()<ZHDatePickerDelegate,UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,SDFormBaseDataDelegate,UITableViewDataSource, UITableViewDelegate>
@property(nonatomic,strong)UITableView* tableView;

@property (nonatomic, strong) ZHDatePicker* picker;
@property (nonatomic, strong) UITextField* IdField;
@property (nonatomic, strong) UILabel* IdHint;
@property (nonatomic, strong) UITextField* nameTextField;
@property (nonatomic, strong) UILabel* birthdayLabel;
@property (nonatomic, strong) SDAvatarView *avatarView;

@property (nonatomic, strong) UIImage* uploadAvatar;
@property (nonatomic, strong) NSString* fileName;
@property (nonatomic, strong) NSString* gender;

@property (nonatomic,strong)SDEditPaientInfoData* editPaientData;
@end

@implementation SDPaientEditorViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initSubviews];
    [self initData];
}

-(void)initData
{
    _editPaientData = [[SDEditPaientInfoData alloc] init];
    _editPaientData.delegate = self;
    
    if (_paientItem == nil) {
        _paientItem = [[SDPaientInfoItem alloc] init];
        _paientItem.gender = @"1";
        _paientItem.pId = [[NSString randomAlphanumericWithLength:8] integerValue];
        _paientItem.cId = [[NSDate date] formattedStringWithFormat:@"yyyyMMddHHmm"];
    }
    
    _fileName = [NSString randomAlphanumericWithLength:8];
}

-(void)initSubviews
{
    BOOL naviHidden = [self navigationBarHidden];
    if (!naviHidden && self.navBar != nil)
    {
        [self.navBar setTitle:@"数据编辑"];
        [self.navBar customNaviLeftButton:self action:@selector(onBack:) title:@"取消"];
        [self.navBar customNaviRightButton:self action:@selector(onStore:) title:@"存储"];
    }

    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _tableView = [[UITableView alloc] initWithFrame:autoRectTableViewHaveBar(YES) style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor colorWithRGB:0xf9fafc];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChanged:) name:UITextFieldTextDidChangeNotification object:_IdField];
  }

-(UIView*)cretateSeparatorLineView
{
    UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(kFiedViewMarginLeftWidth, 0, kScreenWidth - 2*kFiedViewMarginLeftWidth, 1)];
    [separatorLineView setBackgroundColor:[UIColor colorWithRGB:0xcad6e3]];
    return separatorLineView;
}

- (BOOL)navigationBarHidden
{
    return NO;
}

-(void)showImagePickerWithSourceType:(NSInteger)sourceType
{
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    }
    else {
        sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    }
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    imagePickerController.sourceType = sourceType;
    
    [self presentViewController:imagePickerController animated:YES completion:^{}];

}

-(void)uploadPaientInfoWithAvatarUrl:(NSString*)avatarUrl
{
    _paientItem.cId = _IdField.text;
    _paientItem.name = _nameTextField.text;
    _paientItem.dateOfBirth = CHECK_VALID_STRING(_birthdayLabel.text) ? _birthdayLabel.text :@"";
    if (CHECK_VALID_STRING(avatarUrl)) {
        _paientItem.avatar = avatarUrl;
    } else {
        _paientItem.avatar = CHECK_VALID_STRING(_paientItem.avatar) ? _paientItem.avatar :@"";;

    }
    _editPaientData.paientInfoItem = _paientItem;
    
    if (_editable) {
        _editPaientData.editable = _editable;
        [_editPaientData excuteHttpRequest];
    } else {
        [_editPaientData uploadFormDataWithUserInfo:nil];
    }
}

-(void)dismissKeyboard
{
    [_IdField resignFirstResponder];
    [_nameTextField resignFirstResponder];
    [_picker disappear];
}

- (BOOL)cidExist:(NSString *)cid
{
    NSString *sql = [NSString stringWithFormat:@"cid=\"%@\" and pid!=%ld",cid, _paientItem.pId];

    NSArray* data = [[SDPaientInfoMgr shareAGDatabaseManager] select:[SDPaientInfoItem class] tableName:kPaientTableName where:sql];
    if (data == nil || data.count == 0)
        return FALSE;
    return TRUE;
}

- (void)textFieldChanged:(id)sender
{
    if (_IdField.text.length > 20) {
        _IdField.text = [_IdField.text substringToIndex:20];
    }
    
    BOOL flag = [self cidExist:_IdField.text];
    if (flag) {
        self.navBar.editBtn.userInteractionEnabled=NO;
        self.navBar.editBtn.alpha=0.4;
        _IdHint.text = @"ID不能重复";
    }
    else {
        self.navBar.editBtn.userInteractionEnabled=YES;
        self.navBar.editBtn.alpha=1;
        _IdHint.text = @"";
    }
}

#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    if (row == 0) {
        return 116;
    }
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = [indexPath row];
    
    if (row == 0) {
        UITableViewCell* cell = nil;
        static NSString* identifier = @"avatarCell";
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if( cell == nil )
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            _avatarView = [[SDAvatarView alloc] initWithFrame:CGRectMake(0, 20, kAvatarViewWidth, kAvatarViewHeight)];
            _avatarView.left = (self.view.width - kAvatarViewWidth)/2;
            UIImage * defaultImg = [UIImage imageNamed:@"avatar_btn_camera.png"];
            _avatarView.image = defaultImg;
            [cell.contentView addSubview:_avatarView];
            UITapGestureRecognizer* avatarTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onAvatarClicked:)];
            _avatarView.userInteractionEnabled = YES;
            [_avatarView addGestureRecognizer:avatarTapGesture];            
            if (CHECK_VALID_STRING(_paientItem.avatar)) {
                NSString* directory = [SDFile getDirectoryInCacheWithName:kAvatarDirectoryName];
                NSString *filePath = [directory stringByAppendingPathComponent:_paientItem.avatar];
                if ([SDFile fileExist:filePath]) {
                    _avatarView.image = [_avatarView modifiedImageWithImage:[UIImage imageWithContentsOfFile:filePath]];
                }
                else
                {
                    [_avatarView setImageWithURL:nil];
                }
            }
    

            UIView* separatorLine = [self cretateSeparatorLineView];
            separatorLine.width = kScreenWidth;
            separatorLine.left = 0;
            separatorLine.top = _avatarView.bottom + 25;
            [cell.contentView addSubview:separatorLine];
        }
        return cell;
    } else if(row == 1){
        UITableViewCell* cell = nil;
        static NSString* identifier = @"IDCell";
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if( cell == nil )
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            SDCustomFieldView* iDFieldView = [[SDCustomFieldView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
            [iDFieldView setKeyName:@"ID"];
            [cell.contentView addSubview:iDFieldView];
            
            _IdField = [[UITextField alloc] initWithFrame:CGRectMake(iDFieldView.nameLabel.right + kFiedViewMarginLeftWidth, 0, iDFieldView.width - iDFieldView.nameLabel.right - kFiedViewMarginLeftWidth, 44)];
            _IdHint = [[UILabel alloc] initWithFrame:CGRectMake(iDFieldView.nameLabel.right + kFiedViewMarginLeftWidth + 180, 0, iDFieldView.width - iDFieldView.nameLabel.right - kFiedViewMarginLeftWidth, 44)];
            [_IdHint setFont:[UIFont systemFontOfSize:13.f]];
            _IdHint.textColor = [UIColor colorWithRGB:0x12d2c4];
            _IdField.delegate = self;
            _IdField.keyboardType=UIKeyboardTypeNumberPad;
            _IdField.textColor = [UIColor colorWithRGB:0x87909a];
            [_IdField setFont:[UIFont systemFontOfSize:13.f]];
            [iDFieldView addSubview:_IdField];
            [iDFieldView addSubview:_IdHint];
            if (CHECK_VALID_STRING(_paientItem.cId)) {
                _IdField.text = _paientItem.cId;
            }
            
            UIView* separatorLine = [self cretateSeparatorLineView];
            separatorLine.top = 44;
            [cell.contentView addSubview:separatorLine];
        }
        return cell;
        
    } else if(row == 2){
        UITableViewCell* cell = nil;
        static NSString* identifier = @"nameCell";
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if( cell == nil )
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            SDCustomFieldView* nameFieldView = [[SDCustomFieldView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
            [nameFieldView setKeyName:@"姓名"];
            [cell.contentView addSubview:nameFieldView];
            
            _nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(nameFieldView.nameLabel.right + kFiedViewMarginLeftWidth, 0, nameFieldView.width - nameFieldView.nameLabel.right - kFiedViewMarginLeftWidth, 44)];
            _nameTextField.delegate = self;
            _nameTextField.textColor = [UIColor colorWithRGB:0x87909a];
            [_nameTextField setFont:[UIFont systemFontOfSize:13.f]];
            [nameFieldView addSubview:_nameTextField];
            
            if (CHECK_VALID_STRING(_paientItem.name)) {
                _nameTextField.text = _paientItem.name;
            }
            
            UIView* separatorLine = [self cretateSeparatorLineView];
            separatorLine.top = nameFieldView.bottom;
            [cell.contentView addSubview:separatorLine];
        }
        return cell;
        
    } else if(row == 3){
        UITableViewCell* cell = nil;
        static NSString* identifier = @"genderCell";
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if( cell == nil )
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            SDCustomFieldView* genderFieldView = [[SDCustomFieldView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
            [genderFieldView setKeyName:@"性别"];
            [cell addSubview:genderFieldView];
            
            NSArray *segmentedArray = [NSArray arrayWithObjects:@"男",@"女",nil];
            UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentedArray];
            segmentedControl.frame = CGRectMake(genderFieldView.nameLabel.right + kFiedViewMarginLeftWidth, 0.0, 100, 25);
            segmentedControl.selectedSegmentIndex = 0;
            if (CHECK_VALID_STRING(_paientItem.gender)) {
                [_paientItem.gender isEqualToString:@"1"] ? (segmentedControl.selectedSegmentIndex = 0) : (segmentedControl.selectedSegmentIndex = 1);
            }
            segmentedControl.tintColor = [UIColor grayColor];
            [segmentedControl addTarget:self action:@selector(indexDidChangeForSegmentedControl:)
                       forControlEvents:UIControlEventValueChanged];
            segmentedControl.top = (genderFieldView.height - segmentedControl.height)/2;
            [genderFieldView addSubview:segmentedControl];
            
            UIView* separatorLine = [self cretateSeparatorLineView];
            separatorLine.top = genderFieldView.bottom;
            [cell addSubview:separatorLine];
        }
        return cell;
        
    } else if(row == 4){
        UITableViewCell* cell = nil;
        static NSString* identifier = @"dateOfBirthdayCell";
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if( cell == nil )
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            SDCustomFieldView* dateOfBirthdayFieldView = [[SDCustomFieldView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
            [dateOfBirthdayFieldView setKeyName:@"生日"];
            [cell.contentView addSubview:dateOfBirthdayFieldView];
            
            _birthdayLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, dateOfBirthdayFieldView.width - dateOfBirthdayFieldView.nameLabel.right - kFiedViewMarginLeftWidth, 44)];
            _birthdayLabel.textColor = [UIColor colorWithRGB:0xbcbec8];
            [_birthdayLabel setFont:[UIFont systemFontOfSize:13.f]];
            _birthdayLabel.top = (dateOfBirthdayFieldView.height - _birthdayLabel.height)/2;
            _birthdayLabel.left = dateOfBirthdayFieldView.nameLabel.right + 10;
            [dateOfBirthdayFieldView addSubview:_birthdayLabel];
            
            if (CHECK_VALID_STRING(_paientItem.dateOfBirth)) {
                _birthdayLabel.text = _paientItem.dateOfBirth;
            }
            
            UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelectDate:)];
            [tapGesture setNumberOfTouchesRequired:1];
            _birthdayLabel.userInteractionEnabled = YES;
            [_birthdayLabel addGestureRecognizer:tapGesture];
            
            UIView* separatorLine = [self cretateSeparatorLineView];
            separatorLine.width = kScreenWidth;
            separatorLine.top = dateOfBirthdayFieldView.bottom;
            separatorLine.left = 0;
            [cell.contentView addSubview:separatorLine];
        }
        return cell;

    }
    
    return nil;
}

#pragma mark UI Event
- (void)onBack:(id)sender
{
    [self dismissKeyboard];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)onStore:(id)sender
{
    [self dismissKeyboard];    
    // 登录校验
    if (_nameTextField.text.length <= 0) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"姓名不能为空" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        
        return;
    }
    
//    if (_uploadAvatar != nil) {
//        [self showLoading];
//        [[AliyunOSSManager sharedInstance] uploadObjectSyncWithFileName:_fileName image:_uploadAvatar completion:^(id data, NSError *error) {
//            if (!error) {
//                [self uploadPaientInfoWithAvatarUrl:data];
//            }else{
//                [self failedLoading];
//            }
//        }];
//    } else
//    {
//        [self uploadPaientInfoWithAvatarUrl:nil];
//    }
    
    [self onStoreLocalOnly];
}

-(void)onStoreLocalOnly
{
    if (CHECK_VALID_STRING(_IdField.text)) {
        _paientItem.cId = _IdField.text;
    }
    _paientItem.name = _nameTextField.text;
    _paientItem.dateOfBirth = CHECK_VALID_STRING(_birthdayLabel.text) ? _birthdayLabel.text :@"";
   //_paientItem.avatar = @"";
    
    if (_uploadAvatar != nil) {
        [self saveAvatarToLocalFile:_uploadAvatar];
    }
    if (_editable) {
        BOOL success = [[SDPaientInfoMgr shareAGDatabaseManager] update:_paientItem tableName:kPaientTableName where:[NSString stringWithFormat:@"pId = %ld",_paientItem.pId]];
        if (success) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        
    } else {
        BOOL success = [[SDPaientInfoMgr shareAGDatabaseManager] insert:_paientItem tableName:kPaientTableName];
        if (success) {
            [[NSNotificationCenter defaultCenter] postNotificationName:kAddNewPaientSuccessNotification object:nil userInfo:nil];
            if (self.block) {
                self.block(_paientItem);
            }
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }
}

-(BOOL)saveAvatarToLocalFile:(UIImage*)image
{
    UIImage* originalImg =  image;
    
    NSString* identifier = [NSString randomAlphanumericStringWithLength:10];
    NSString* originalImgFileName = [NSString stringWithFormat:@"%@.jpg",identifier];
    NSString* directory = [SDFile getDirectoryInCacheWithName:[NSString stringWithFormat:@"%@",kAvatarDirectoryName]];
    NSString *originalImgFilePath = [directory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",originalImgFileName]];
    
    //NSData *data = UIImageJPEGRepresentation(originalImg, 0.4);
    NSData *data = UIImagePNGRepresentation(originalImg);
    _paientItem.avatar = originalImgFileName;
    return [[NSFileManager defaultManager] createFileAtPath:originalImgFilePath contents:data attributes:nil];
}

-(void)onAvatarClicked:(UITapGestureRecognizer*)recognizer
{
    UIAlertController *photoController =   [UIAlertController
                                             alertControllerWithTitle:@""
                                             message:nil
                                             preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *takePhotoAction = [UIAlertAction
                                     actionWithTitle:@"拍照"
                                     style:UIAlertActionStyleDefault
                                     handler:^(UIAlertAction * action)
                                     {
                                         [self showImagePickerWithSourceType:UIImagePickerControllerSourceTypeCamera];
                                         [photoController dismissViewControllerAnimated:YES completion:nil];
                                         
                                     }];
    UIAlertAction *localPhotoAction = [UIAlertAction
                                        actionWithTitle:@"从相册选择"
                                        style:UIAlertActionStyleDefault
                                        handler:^(UIAlertAction * action)
                                        {
                                            [self showImagePickerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
                                            [photoController dismissViewControllerAnimated:YES completion:nil];
                                            
                                        }];
    UIAlertAction *cancelAction = [UIAlertAction
                                   actionWithTitle:@"取消"
                                   style:UIAlertActionStyleDestructive
                                   handler:^(UIAlertAction * action)
                                   {
                                       //Do Whatever you want to do here when click this button
                                       [photoController dismissViewControllerAnimated:YES completion:nil];
                                       
                                   }];
    
    [photoController addAction:takePhotoAction];
    [photoController addAction:localPhotoAction];
    [photoController addAction:cancelAction];
    
    [self presentViewController:photoController animated:YES completion:nil];
}

-(void)onSelectDate:(UITapGestureRecognizer*)recognizer
{
    [_IdField resignFirstResponder];
    [_nameTextField resignFirstResponder];

    if (nil == _picker) {
        
        ZHDatePicker *picker = [ZHDatePicker presentOnWindow];
        [picker setDelegate:self];
       // [picker setPickerDisplayMode:ZHDatePickerDisplayModelBeforeCurrent];
        _picker = picker;
    }
    [_picker present];
}

-(void)indexDidChangeForSegmentedControl:(id)sender
{
    UISegmentedControl* segmentControl = (UISegmentedControl*)sender;
    if(segmentControl.selectedSegmentIndex == 0){
        _paientItem.gender = @"1";
    }else{
        _paientItem.gender = @"2";
    }
}
#pragma mark - ZH date Picker Delegate
- (void)didSelectDateWithDate:(NSDate *)date year:(NSInteger)year month:(NSInteger)month day:(NSInteger)day {
    if (nil == date) {
        return;
    }
    
    if (_birthdayLabel != nil) {
        [_birthdayLabel setText:[NSString stringWithFormat:@" %@-%@-%@", @(year), @(month), @(day)]];
        [_birthdayLabel sizeToFit];
        _birthdayLabel.top = (_birthdayLabel.superview.height - _birthdayLabel.height)/2;
    }
}

#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
    UIImage* resizeImg = [SDImage imageWithImage:image scaledToWidth:kAvatarViewWidth*2];
    NSData *data = nil;
    //判断图片是不是png格式的文件
    if (UIImagePNGRepresentation(resizeImg)) {
        //返回为png图像。
        data = UIImagePNGRepresentation(resizeImg);
    }else {
        //返回为JPEG图像。
        data = UIImageJPEGRepresentation(resizeImg, 0.4);
    }
    
    _avatarView.image = [_avatarView modifiedImageWithImage:resizeImg];
    _uploadAvatar = _avatarView.image;
}

- (void)showLoading{
    [self finishLoading];
    [VPMBProgressHUD showIndicatorWithText:@"发送中"];
}

- (void)finishLoading
{
    [VPMBProgressHUD hideIndicator];
}

- (void)failedLoading
{
    [self finishLoading];
    [VPMBProgressHUD showAttendAlert:@"上传失败" andIcon:nil];
}

#pragma mark - UIScrollView
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self dismissKeyboard];
}

#pragma mark- SDFromBaseDatDelegate
-(void) willUploadOfFormData:(SDFormBaseData*) formdata
{
    if ([formdata isKindOfClass:[SDEditPaientInfoData class]])
    {
        [self showLoading];
    }

}
-(void) formData:(SDFormBaseData*)formdata userInfo:(NSDictionary*) userInfo didFinishLoadWithError:(NSError*)error
{
    [self finishLoading];
    if (!error) {
        if (_editable) {
            BOOL success = [[SDPaientInfoMgr shareAGDatabaseManager] update:_editPaientData.paientInfoItem tableName:kPaientTableName where:[NSString stringWithFormat:@"pId = %ld",_editPaientData.paientInfoItem.pId]];
            if (success) {
                _paientItem = _editPaientData.paientInfoItem;
                [self dismissViewControllerAnimated:YES completion:nil];
            }
            
        } else {
            BOOL success = [[SDPaientInfoMgr shareAGDatabaseManager] insert:_editPaientData.paientInfoItem tableName:kPaientTableName];
            if (success) {
                [[NSNotificationCenter defaultCenter] postNotificationName:kAddNewPaientSuccessNotification object:nil userInfo:nil];
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }
       
    }else{
        [VPMBProgressHUD showAttendAlert:[error.userInfo objectForKey:@"SDHttpRequest_ResponseInfo"] andIcon:nil];
    }
}

- (void)didSelectedItem:(SelectedItem)block {
    self.block = block;
}
@end
